#!/bin/sh
#
# resize -- allow resize remote screen size
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#
# TODO: Fix up noVNC remote resizing feature
#

# Allow pass first argument as SCREEN_SIZE
if [ -n "$1" ]; then
  echo $1 | grep -E -q '[0-9]{3,}x[0-9]{3,}|^auto$|^fullscreen$'
  if [ $? -eq 0 ]; then
    SCREEN_SIZE=$1
    shift
  else
    LAB_NAME=$1
  fi
fi

# Allow pass second argument as SCREEN_SIZE, but first one as lab name
if [ -n "$2" ]; then
  echo $2 | grep -E -q '[0-9]{3,}x[0-9]{3,}|^auto$|^fullscreen$'
  if [ $? -eq 0 ]; then
    SCREEN_SIZE=$2
  fi
fi

if [ -z "$SCREEN_SIZE" ]; then
  if [ -z "$SCREEN_RESIZE" ]; then
    SCREEN_SIZE=auto
  else
    SCREEN_SIZE=$SCREEN_RESIZE
  fi
fi

if [ "$SCREEN_SIZE" = "auto" -o "$SCREEN_SIZE" = "fullscreen" ]; then
  resize_type=$SCREEN_SIZE
  SCREEN_SIZE=""

  which xrandr >/dev/null 2>&1
  ret=$?

  if [ $ret -eq 0 ]; then
    SCREEN_SIZE=`xrandr --current 2>/dev/null | grep "^ .*\*" | tr -s ' ' | cut -d' ' -f2 | cut -d '_' -f1 | head -1 | tr -d -c '[0-9x]'`
    ret=$?
  fi

  if [ $ret -eq 0 -a "$resize_type" = "auto" ]; then
    current_height=`echo $SCREEN_SIZE | cut -d 'x' -f2 | tr -d -c '[0-9]'`
    for h in `xrandr --current 2>/dev/null | grep "^ [ 0-9]*x" | cut -d'x' -f2 | cut -d' ' -f1`
    do
      h=$(echo $h | tr -d -c '[0-9]')
      [ $h -lt $current_height ] && break
    done
    SCREEN_SIZE=`xrandr --current 2>/dev/null | grep "^ [ 0-9]*x"  | grep "x$h" | tr -s ' ' | cut -d' ' -f2 | cut -d '_' -f1 | head -1 | tr -d -c '[0-9x]'`
  fi

  which wmic >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    if [ -z "$SCREEN_SIZE" ]; then
      SCREEN_SIZE=$(wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value | sed -ne "s/.*=\([0-9]*\)/\1/gp" | tr '\r\n' 'x' | tr -s 'x' | cut -d'x' -f1-2 | tr -d '\r\n')
      ret=$?
    fi
  fi

  if [ $ret -ne 0 -o "$SCREEN_SIZE" = "" ]; then
    echo "Usage: $0 screen_widthxscreen_height"
    echo "   ex. $0 1920x1080"
    exit 1
  fi
fi

TOP_DIR="$(cd "$(dirname "$0")"/../../ && pwd)"
. "$TOP_DIR"/tools/docker/config >/dev/null

lab_id=$(docker ps -f name=$CONTAINER_NAME -f status=running -q)
[ -z "$lab_id" ] && err_print "$LAB_NAME is not running." && exit 1

[ -z "$UNIX_USER" ] && get_var UNIX_USER $DEF_USER
[ "x$HOST_OS" = x"Windows" ] && WINPTY="winpty"

# wait for a while till the screen size is really changed
i=0
while [ $i -lt 10 ];
do
  # If want to call this in desktop shortcut, must remove the -t option, otherwise, it will simply silence without any info and any action
  do_op "$WINPTY docker exec -i -w $LAB_WORKDIR/$CURRENT -u $UNIX_USER" CONTAINER_NAME "x11vnc -display :1 -R scale:$SCREEN_SIZE"

  sleep 2

  scale=$(do_op "docker exec -i -w $LAB_WORKDIR/$CURRENT -u $UNIX_USER" CONTAINER_NAME "x11vnc -display :1 -query scale" | cut -d':' -f2)

  if [ "$scale" = "$SCREEN_SIZE" ]; then
    info_print "The screen size is changed to $scale sucessfully"
    break
  else
    info_print "The screen size is $scale, not changed to $SCREEN_SIZE"
  fi
  i=`expr $i + 1`
done

# Record SCREEN_SIZE setting
do_unlock
set_var SCREEN_SIZE
do_lock
